package com.tanhua.dubbo.api;

import com.tanhua.dubbo.api.mongo.TestSoulApi;
import com.tanhua.model.mongo.*;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.BulkOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 测灵魂处理类
 */
@DubboService
public class TestSoulApiImpl implements TestSoulApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 查询所有符合条件的选项
     * @param optionIds
     * @return
     */
    @Override
    public List<Option> findScores(List<ObjectId> optionIds) {
        Query query=new Query();
        query.addCriteria(Criteria.where("id").in(optionIds));
        return mongoTemplate.find(query, Option.class);
    }

    /**
     * 查询问卷的id
     * @param questionId
     * @return
     */
    @Override
    public Questionnaire getQuestionnaire(ObjectId questionId) {
        Query query=new Query();
        query.addCriteria(Criteria.where("id").is(questionId));
        Question one = mongoTemplate.findOne(query, Question.class);

        Query query2=new Query();
        query2.addCriteria(Criteria.where("id").is(one.getQuestionnaireId()));
        return mongoTemplate.findOne(query2, Questionnaire.class);
    }
    /**
     * 修改或增加报告并返回
     * @param report
     * @return
     */
    @Override
    public Report addAndUpdateReport(Report report) {
        Query query=new Query();
        query.addCriteria(Criteria.where("userId").is(report.getUserId()));
        Report one = mongoTemplate.findOne(query, Report.class);
            //如果查询结果为空则添加
        Report newReport=null;
        if(StringUtils.isEmpty(one)){
            newReport = mongoTemplate.insert(report);
        }else {

            Update update=new Update();
            update.set("questionnaireId",report.getQuestionnaireId());
            update.set("conclusion",report.getConclusion());
            update.set("score",report.getScore());
            update.set("cover",report.getCover());
            update.set("level",report.getLevel());
            update.set("updated",report.getUpdated());
            newReport = mongoTemplate.findAndModify(query, update, Report.class);
        }

        return newReport;
    }

    /**
     * 修改或增加维度
     *
     * @param dimensionList
     * @return
     */
    @Override
    public void addAndUpdateDimension(List<Dimension> dimensionList) {
        Query query=new Query();
        query.addCriteria(Criteria.where("ObjectId").is(dimensionList.get(0).getReportId()));
        List<Dimension> dimensions = mongoTemplate.find(query, Dimension.class);

        if(!CollectionUtils.isEmpty(dimensions)) {
            //不为空 则先删除原来的记录
            mongoTemplate.remove(query, Dimension.class);
        }

        // 批量插入
        BulkOperations ops = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, Dimension.class);
        ops.insert(dimensionList);
    }

    @Override
    public Report findResult(String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(id));
        return mongoTemplate.findOne(query, Report.class);
    }
}
